package org.freespace.controller;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.freespace.Constant;
import org.freespace.model.JmxParamfile;
import org.freespace.model.JmxPlugin;
import org.freespace.model.JmxScriptPlugin;
import org.freespace.service.IJmxParamfileService;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.response.CommonResult;
import org.freespace.vo.JmxParamfileVO;
import org.freespace.vo.JmxPluginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 * jmx参数化文件表 前端控制器
 * </p>
 *
 * @author liuqi
 * @since 2023-05-12
 */
@RestController
@RequestMapping("/jmx-paramfile")
public class JmxParamfileController {
    @Autowired
    IJmxParamfileService paramfileService;

    /**
     * 上传插件文件
     * @param file
     * @param jmxParamfile
     * @return
     * @throws IOException
     */
    @PostMapping("addParamFile")
    public CommonResult addParamFile(@RequestPart("file") MultipartFile[] file, @RequestPart("jmxParamfile") JmxParamfile jmxParamfile) throws IOException {
        paramfileService.addParamFile(file, jmxParamfile.getScriptId());
        return CommonResult.success();
    }

    @PostMapping("bind")
    public CommonResult bind(@RequestBody JmxParamfileVO vo) {

        IPage<JmxParamfile> jmxParamfileIPage = paramfileService.bindPageList(vo);
        return CommonResult.success(jmxParamfileIPage);
    }


    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable("id") int id) {
        JmxParamfile paramfile = paramfileService.getById(id);
        FileUtil.del(new File(Constant.PARAM_FILE_SERVER_DIR + "/" + id+"/"+paramfile.getFileName()));
        paramfileService.removeById(id);

        return CommonResult.success();
    }
}
